<template>
  <splitpanes class="default-theme" style="height: auto">
    <pane :min-size="split.minWidth" :size="split.minWidth">
      <a-card
        @contextDictionary.prevent
        size="small"
        class="eip-admin-card-small"
      >
        <div slot="title">
          <a-input-search
            v-model="menu.key"
            :allowClear="true"
            placeholder="名称/拼音模糊搜索"
            @change="menuSearch"
          />
        </div>
        <a-spin :spinning="menu.spinning" :delay="0" :style="menu.style">
          <div>
            <a-directory-tree
              v-if="!menu.spinning"
              :defaultExpandedKeys="[
                menu.data.length > 0 ? menu.data[0].key : '',
              ]"
              :expandAction="false"
              :tree-data="menu.data"
              :icon="menuIcon"
              @select="menuSelect"
            ></a-directory-tree>
          </div>
        </a-spin>
      </a-card>
    </pane>
    <pane min-size="50">
      <vxe-grid v-bind="grid" ref="xGrid">
        <template #toolbar_buttons>
          <eip-toolbar
            @del="del"
            @update="update"
            @add="add"
            @copy="copy"
            @detail="detailClick"
            @onload="toolbarOnload"
          ></eip-toolbar>
        </template>
        <template #toolbar_tools> </template>
        <template #loading>
          <a-spin></a-spin>
        </template>
        <template #empty>
          <eip-empty />
        </template>
        <template #checkboxHeader="{ checked, indeterminate }">
          <span @click.stop="$refs.xGrid.toggleAllCheckboxRow()">
            <a-checkbox :indeterminate="indeterminate" :checked="checked">
            </a-checkbox>
          </span>
        </template>
        <template #checkbox="{ row, checked, indeterminate }">
          <span @click.stop="$refs.xGrid.toggleCheckboxRow(row)">
            <a-checkbox :indeterminate="indeterminate" :checked="checked">
            </a-checkbox>
          </span>
        </template>
        <template #isFreeze="{ row }">
          <a-switch :checked="row.isFreeze" @change="isFreezeChange(row)" />
        </template>
        <template #isShowTable="{ row }">
          <a-switch
            :checked="row.isShowTable"
            @change="isShowTableChange(row)"
          />
        </template>
        <template #icon="{ row }">
          <a-icon v-if="row.icon" :type="row.icon"></a-icon>
        </template>
        <template #operate="{ row }">
          <a-tooltip
            @click="tableUpdateRow(row)"
            title="编辑"
            v-if="visible.update"
          >
            <label class="text-eip eip-cursor-pointer">编辑</label>
          </a-tooltip>
          <a-divider type="vertical" v-if="visible.update" />
          <a-tooltip title="删除" v-if="visible.del" @click="tableDelRow(row)">
            <label class="text-red eip-cursor-pointer">删除</label>
          </a-tooltip>
        </template>
      </vxe-grid>
    </pane>
    <edit
      ref="edit"
      v-if="edit.visible"
      :visible.sync="edit.visible"
      :data="menu.data"
      :title="edit.title"
      :copy="edit.copy"
      :menuButtonId="edit.menuButtonId"
      @save="operateStatus"
    ></edit>

    <permission-detail
      ref="detail"
      v-if="detail.visible"
      :visible.sync="detail.visible"
      :title="detail.title"
      :id="detail.id"
      :access="detail.access"
    ></permission-detail>
  </splitpanes>
</template>

<script>
import {
  menuQuery,
  query,
  del,
  isFreeze,
  isShowTable,
} from "@/services/system/menubutton/list";
import edit from "./edit";
import permissionDetail from "@/pages/system/permission/detail";

import { selectTableRow, deleteConfirm } from "@/utils/util";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";
export default {
  components: {
    edit,
    permissionDetail,
    Splitpanes,
    Pane,
  },
  data() {
    return {
      split: {
        minWidth: (250 / window.innerWidth) * 100,
      },
      menu: {
        style: {
          overflow: "auto",
          height: this.eipTableHeight() - 58 + "px",
        },
        key: null,
        data: [],
        original: [],
        spinning: true,
        right: {
          item: null,
          style: "",
        },
      },
      grid: {
        id: "notice",
        loading: true,
        height: this.eipTableHeight(),
        sortConfig: {
          trigger: "cell",
          defaultSort: { field: "orderNo", order: "asc" },
          orders: ["desc", "asc", null],
          remote: true,
          multiple: true,
          chronological: true,
        },
        customConfig: {
          mode: "popup",
        },
        pagerConfig: {},
        printConfig: {},
        exportConfig: {},
        filterConfig: {
          remote: true,
        },
        toolbarConfig: {
          slots: {
            buttons: "toolbar_buttons",
            tools: "toolbar_tools",
          },
        },
        columns: [
          {
            type: "checkbox",
            width: 50,
            slots: {
              checkbox: "checkbox",
              header: "checkboxHeader",
            },
            fixed: "left",
            align: "center",
          },
          { type: "seq", width: 60, fixed: "left" },
          {
            field: "name",
            title: "名称",
            width: 120,
            sortable: true,
            showOverflow: "ellipsis",
            filterRender: {
              name: "MLFilterRender",
            },
            filters: [
              {
                data: {
                  type: "input",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "menuNames",
            title: "归属菜单",
            width: 220,
            sortable: true,
            showOverflow: "ellipsis",
            filterRender: {
              name: "MLFilterRender",
            },
            filters: [
              {
                data: {
                  type: "input",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "icon",
            title: "图标",
            width: 120,
            sortable: true,
            align: "center",
            filterRender: {
              name: "MLFilterRender",
            },
            slots: {
              default: "icon",
            },
            filters: [
              {
                data: {
                  type: "icon",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "method",
            title: "事件",
            width: 120,
            sortable: true,
            showOverflow: "ellipsis",
            filterRender: {
              name: "MLFilterRender",
            },
            filters: [
              {
                data: {
                  type: "input",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "isFreeze",
            title: "禁用",
            width: 120,
            sortable: true,
            showOverflow: true,
            align: "center",
            slots: {
              default: "isFreeze",
            },
            filterRender: { name: "MLFilterRender" },
            filters: [
              {
                data: {
                  type: "switch",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [
                    {
                      value: 1,
                      label: "已禁用",
                      checked: false,
                      color: "red",
                    },
                    {
                      value: 0,
                      label: "未禁用",
                      checked: false,
                      color: "#1890ff",
                    },
                  ],
                },
              },
            ],
          },
          {
            field: "isShowTable",
            title: "列表显示",
            width: 140,
            sortable: true,
            align: "center",
            showOverflow: true,
            slots: {
              default: "isShowTable",
            },
            filterRender: { name: "MLFilterRender" },
            filters: [
              {
                data: {
                  type: "switch",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [
                    {
                      value: 1,
                      label: "已禁用",
                      checked: false,
                      color: "red",
                    },
                    {
                      value: 0,
                      label: "未禁用",
                      checked: false,
                      color: "#1890ff",
                    },
                  ],
                },
              },
            ],
          },
          {
            field: "remark",
            title: "备注",
            width: 120,
            sortable: true,
            filterRender: { name: "MLFilterRender" },
            filters: [
              {
                data: {
                  type: "input",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "orderNo",
            title: "排序号",
            width: 120,
            sortable: true,
            filterRender: { name: "MLFilterRender" },
            filters: [
              {
                data: {
                  type: "number",
                  op: "cn",
                  write: null,
                  min: null,
                  max: null,
                },
              },
            ],
          },
          {
            field: "createUserName",
            title: "创建人",
            width: 150,
            sortable: true,
            filterRender: {
              name: "MLFilterRender",
            },
            filters: [
              {
                data: {
                  type: "input",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "createTime",
            title: "创建时间",
            width: 150,
            sortable: true,
            filterRender: { name: "MLFilterRender" },
            filters: [
              {
                data: {
                  type: "datetime",
                  op: "cn",
                  write: null,
                  min: null,
                  max: null,
                },
              },
            ],
          },
          {
            field: "updateUserName",
            title: "修改人",
            width: 150,
            sortable: true,
            filterRender: {
              name: "MLFilterRender",
            },
            filters: [
              {
                data: {
                  type: "input",
                  key: null,
                  op: "cn",
                  write: null,
                  options: [],
                },
              },
            ],
          },
          {
            field: "updateTime",
            title: "修改时间",
            width: 150,
            sortable: true,
            filterRender: { name: "MLFilterRender" },
            filters: [
              {
                data: {
                  type: "datetime",
                  op: "cn",
                  write: null,
                  min: null,
                  max: null,
                },
              },
            ],
          },
          {
            title: "操作",
            width: 100,
            align: "center",
            fixed: "right",
            slots: {
              default: "operate",
            },
          },
        ],

        proxyConfig: {
          seq: true, // 启用动态序号代理，每一页的序号会根据当前页数变化
          sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
          filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为
          props: {
            result: "data", // 配置响应结果列表字段
            total: "total", // 配置响应结果总页数字段
          },
          ajax: {
            query: ({ page, sorts, filters }) => {
              return new Promise((resolve) => {
                const firstSort = sorts[0];
                query({
                  filters: this.eipTableFilter(filters),
                  current: page.currentPage,
                  size: page.pageSize,
                  id: this.grid.extend.id,
                  sort: this.eipGridSort(sorts),
                }).then((result) => {
                  if (result.code == this.eipResultCode.success) {
                    resolve(result);
                  }
                  this.grid.loading = false;
                });
              });
            },
          },
        },
        data: [],
        extend: {
          id: null,
        },
      },

      edit: {
        visible: false,
        menuButtonId: null,
        title: null,
        copy: false,
      },

      detail: {
        visible: false,
        title: null,
        id: null,
        access: 1,
      },

      visible: {
        update: false,
        del: false,
      },
    };
  },
  created() {
    this.menuInit();
  },
  methods: {
    menuSearch(e) {
      var that = this;
      this.menu.data = this.$utils.clone(this.menu.original, true);
      this.menu.data = this.$utils.searchTree(
        this.menu.data,
        (item) => {
          if (that.menu.key) {
            var titlePinyin = pinyin.convert(item.title).toLowerCase();
            if (
              item.title.toLowerCase().indexOf(that.menu.key.toLowerCase()) > -1
            ) {
              return true;
            } else if (titlePinyin.indexOf(that.menu.key.toLowerCase()) > -1) {
              return true;
            } else {
              return false;
            }
          } else {
            return true;
          }
        },
        { children: "children" }
      );
    },
    /**
     *冻结
     */
    isFreezeChange(row) {
      if (this.visible.update) {
        let that = this;
        this.$message.loading("保存中...", 0);
        isFreeze({ id: row.menuButtonId }).then((result) => {
          that.$message.destroy();
          if (result.code == that.eipResultCode.success) {
            that.menuButtonInit();
            that.$message.success(result.msg);
          } else {
            that.$message.error(result.msg);
          }
        });
      }
    },

    /**
     * 是否显示到列表
     * @param {*} row
     */
    isShowTableChange(row) {
      if (this.visible.update) {
        let that = this;
        this.$message.loading("保存中...", 0);
        isShowTable({ id: row.menuButtonId }).then((result) => {
          that.$message.destroy();
          if (result.code == that.eipResultCode.success) {
            that.menuButtonInit();
            that.$message.success(result.msg);
          } else {
            that.$message.error(result.msg);
          }
        });
      }
    },

    /**
     * 菜单树
     */
    menuInit() {
      let that = this;
      that.menu.spinning = true;
      menuQuery(that.eipPrivilegeAccess.menubutton).then((result) => {
        that.menu.data = result.data;
        that.menu.original = result.data;
        that.menu.spinning = false;

        that.menuSearch();
      });
    },

    /**
     * 树图标
     */
    menuIcon(props) {
      const { slots } = props;
      return <a-icon type={slots.icon} theme={slots.theme} />;
    },

    /**
     * 树选中
     */
    menuSelect(electedKeys, e) {
      this.grid.extend.id = electedKeys[0];
      this.menuButtonInit();
    },

    /**
     * 列表数据
     */
    menuButtonInit() {
      this.$refs.xGrid.commitProxy("query");
    },

    /**
     * 新增
     */
    add() {
      this.edit.menuButtonId = null;
      this.edit.title = "新增模块按钮";
      this.edit.visible = true;
      this.edit.copy = false;
    },

    /**
     * 修改
     */
    update() {
      let that = this;
      selectTableRow(
        that.$refs.xGrid,
        function (row) {
          that.edit.menuButtonId = row.menuButtonId;
          that.edit.copy = false;
          that.edit.title = "编辑模块按钮-" + row.name;
          that.edit.visible = true;
        },
        that
      );
    },

    /**
     * 复制
     */
    copy() {
      let that = this;
      selectTableRow(
        that.$refs.xGrid,
        function (row) {
          that.edit.menuButtonId = row.menuButtonId;
          that.edit.copy = true;
          that.edit.title = "复制模块按钮-" + row.name;
          that.edit.visible = true;
        },
        that
      );
    },

    /**
     * 删除
     */
    del() {
      let that = this;
      selectTableRow(
        that.$refs.xGrid,
        function (rows) {
          //提示是否删除
          deleteConfirm(
            that.eipMsg.delete,
            function () {
              //加载提示
              that.$loading.show({ text: that.eipMsg.delloading });
              let ids = that.$utils.map(rows, (item) => item.menuButtonId);
              del({ id: ids.join(",") }).then((result) => {
                that.$loading.hide();
                if (result.code == that.eipResultCode.success) {
                  that.menuButtonInit();
                  that.$message.success(result.msg);
                } else {
                  that.$message.error(result.msg);
                }
              });
            },
            that
          );
        },
        that,
        false
      );
    },

    //提示状态信息
    operateStatus(result) {
      if (result.code === this.eipResultCode.success) {
        this.menuButtonInit();
      }
    },

    /**
     * 权限按钮加载完毕
     */
    toolbarOnload(buttons) {
      this.visible.update =
        buttons.filter((f) => f.method == "update").length > 0;
      this.visible.del = buttons.filter((f) => f.method == "del").length > 0;
    },

    /**
     * 树更新
     */
    tableUpdateRow(row) {
      this.edit.menuButtonId = row.menuButtonId;
      this.edit.title = "模块按钮编辑-" + row.name;
      this.edit.visible = true;
    },
    /**
     * 删除
     */
    tableDelRow(row) {
      let that = this;
      deleteConfirm(
        "模块按钮【" + row.name + "】" + that.eipMsg.delete,
        function () {
          that.$loading.show({ text: that.eipMsg.delloading });
          del({ id: row.menuButtonId }).then((result) => {
            that.$loading.hide();
            if (result.code == that.eipResultCode.success) {
              that.menuButtonInit();
              that.$message.success(result.msg);
            } else {
              that.$message.error(result.msg);
            }
          });
        },
        that
      );
    },

    /**
     *详情点击
     */
    detailClick() {
      let that = this;
      selectTableRow(
        that.$refs.xGrid,
        function (row) {
          that.detail.id = row.menuButtonId;
          that.detail.title = "模块按钮使用详情-" + row.name;
          that.detail.visible = true;
        },
        that
      );
    },
  },
};
</script>
